package org.jl.main;

import org.jl.main.model.DefaultValue;
import org.jl.main.model.User;
import org.jl.main.model.vo.UserVO;
import org.jl.main.util.UtilForBean;
import org.jl.main.util.UtilForObject;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

/**
 * java内置对象
 */
public class Main01_Object {

    /*
    java内置对象默认值
    注：强制要求对象属性使用包装对象，属性值默认为空
    注：char的默认值是空字符，是个网页不识别的特殊字符，这里用'null'指代
    运行结果：
    DefaultValue(char1='null', char2=null, bool1=false, byte1=0, short1=0, int1=0, long1=0, float1=0.0, double1=0.0)
     */
    public static void main0(String[] args) {
        System.out.println(new DefaultValue());
    }

    /*
    java内置对象-字符、字符串
    运行结果：
    c2 value is:a
    c2 is equal a:true
    c1 is null:true
    c1 is not null:false

    s1 is null:true
    s1 is not null:false
    s1 is null:true
    s1 is not null:false
    s1 is equal "s1":true
    s1 length is:2
    s1 reverse is:1s
    s1:
    s1:s1
     */
    public static void main1(String[] args) {
        char c2 = 'a';
        System.out.println("c2 value is:" + c2);
        System.out.println("c2 is equal a:" + UtilForObject.isEqual('a', c2));
        //字符
        //长度为1
        Character c1 = null;
        System.out.println("c1 is null:" + UtilForObject.isEmpty(c1));
        System.out.println("c1 is not null:" + UtilForObject.isNotEmpty(c1) + "\n");

        //字符串
        //对应数据库char(n)-定长-max255
        //对应数据库varchar(n)-变长（推荐255及以内，相比text更推荐varchar）-max65535
        //无原始类型
        String s1 = null;
        System.out.println("s1 is null:" + UtilForObject.isEmpty(s1));
        System.out.println("s1 is not null:" + UtilForObject.isNotEmpty(s1));
        //排除空格后判断
        System.out.println("s1 is null:" + UtilForObject.isBlank(s1));
        System.out.println("s1 is not null:" + UtilForObject.isNotBlank(s1));
        s1 = "s1";
        System.out.println("s1 is equal \"s1\":" + "s1".equals(s1));
        System.out.println("s1 length is:" + s1.length());
        //翻转
        System.out.println("s1 reverse is:" + new StringBuilder(s1).reverse());
        s1 = null;
        System.out.println("s1:" + UtilForObject.defaultValue(s1, ""));
        s1 = "s1";
        System.out.println("s1:" + UtilForObject.defaultValue(s1, "") + "\n");
    }

    /*
    java内置对象-布尔
    运行结果：
    b2 value is:true
    b1 is null:true
    b1 is equal false:false
    b1 is equal true:true
     */
    public static void main2(String[] args) {
        //对应数据库tinyint(1)，tinyint最大存储长度255
        boolean b2 = true;
        System.out.println("b2 value is:" + b2);
        Boolean b1 = null;
        System.out.println("b1 is null:" + UtilForObject.isEmpty(b1));
        //注：判断假的时候，null也不是假
        System.out.println("b1 is equal false:" + UtilForObject.isFalse(b1));
        b1 = true;
        System.out.println("b1 is equal true:" + UtilForObject.isTrue(b1) + "\n");
    }

    /*
    java内置对象-整数
    b4 value is:127
    b3 is null:true
    b3 is equal 1:true

    s3 value is:32767
    s2 is null:true
    s2 is equal 1:true

    i2 value is:2100000000
    i1 is null:true
    i1 is equal 1:true

    l2 value is:9220000000000000000
    l1 is null:true
    l1 is equal 1:true
     */
    public static void main3(String[] args) {
        //对应数据库tinyint
        byte b4 = 127;
        System.out.println("b4 value is:" + b4);
        Byte b3 = null;
        System.out.println("b3 is null:" + UtilForObject.isEmpty(b3));
        b3 = 1;
        System.out.println("b3 is equal 1:" + UtilForObject.isEqual((byte) 1, b3) + "\n");

        //对应数据库smallint
        short s3 = 32767;
        System.out.println("s3 value is:" + s3);
        Short s2 = null;
        System.out.println("s2 is null:" + UtilForObject.isEmpty(s2));
        s2 = 1;
        System.out.println("s2 is equal 1:" + UtilForObject.isEqual((short) 1, s2) + "\n");

        //对应数据库int（推荐）
        int i2 = 2100000000;
        System.out.println("i2 value is:" + i2);
        Integer i1 = null;
        System.out.println("i1 is null:" + UtilForObject.isEmpty(i1));
        i1 = 1;
        System.out.println("i1 is equal 1:" + UtilForObject.isEqual(1, i1) + "\n");

        //对应数据库bigint（大数据量表主键推荐）
        long l2 = 9220000000000000000L;
        System.out.println("l2 value is:" + l2);
        Long l1 = null;
        System.out.println("l1 is null:" + UtilForObject.isEmpty(l1));
        l1 = 1L;
        System.out.println("l1 is equal 1:" + UtilForObject.isEqual((long) 1, l1) + "\n");
    }

    /*
    java内置对象-浮点数
    f2 value is:0.0
    f1 is null:true
    f1 is equal 1:true

    d2 value is:0.0
    d1 is null:true
    d1 is equal 1:true

    bd is null:true
    bd is equal 1:true
     */
    public static void main4(String[] args) {
        //对应数据库float
        float f2 = 0;
        System.out.println("f2 value is:" + f2);
        Float f1 = null;
        System.out.println("f1 is null:" + UtilForObject.isEmpty(f1));
        f1 = 1f;
        System.out.println("f1 is equal 1:" + UtilForObject.isEqual((float) 1, f1) + "\n");

        //对应数据库double
        double d2 = 0;
        System.out.println("d2 value is:" + d2);
        Double d1 = null;
        System.out.println("d1 is null:" + UtilForObject.isEmpty(d1));
        d1 = 1d;
        System.out.println("d1 is equal 1:" + UtilForObject.isEqual((double) 1, d1) + "\n");

        //math包，数学计算
        //对应数据库decimal，高精度浮点数（推荐）
        BigDecimal bd = null;
        System.out.println("bd is null:" + UtilForObject.isEmpty(bd));
        bd = new BigDecimal(1);
        System.out.println("bd is equal 1:" + UtilForObject.isEqual((double) 1, bd) + "\n");
    }

    /*
    java内置对象-对象
    o1 is null:true
    bd is equal 1:false
    bd is equal 1:false
    bd is equal 1:true
     */
    public static void main5(String[] args) {
        Object o1 = null;
        System.out.println("o1 is null:" + UtilForObject.isEmpty(o1));
        //equals方法不同类型返回false
        o1 = "1";
        Integer i1 = 1;
        System.out.println("bd is equal 1:" + i1.equals(o1));
        o1 = 1;
        System.out.println("bd is equal 1:" + "1".equals(o1));
        Integer i2 = (Integer) o1;
        System.out.println("bd is equal 1:" + i2.equals(o1) + "\n");
    }

    /*
    java内置对象-日期、时间
    d3 before d4:true
    d3 after d4:false

    c3 before c4:true
    c3 after c4:false
    c3 after c4:true

    source time:2025-02-13T17:33:58.225695
    time1 before time2:true
    time1 after time2:false
    time add 1 day:2025-02-14T17:33:58.225695
    source time:2025-02-13T17:33:58.225695
    format datetime:2025-02-13 17:33:58
    parse datetime:2025-02-13T17:33:58
    format date:2025-02-13
    format time:17:33:58

    获取时间段的秒数：60

    1739439239238->1739525639238
     */
    public static void main6(String[] args) {
//        //util包，时间计算
//        //对应数据库datetime
//        //单位为毫秒
//        Date d3 = new Date();
//        Thread.sleep(1000);
//        Date d4 = new Date();
//        System.out.println("d3 before d4:" + d3.before(d4));
//        System.out.println("d3 after d4:" + d3.after(d4));
//        String d3str = UtilForObject.format(d3);
//        System.out.println("d3 format time:" + d3str);
//        System.out.println("d3 format time:" + UtilForObject.parse(d3str) + "\n");
//
//        Calendar c3 = Calendar.getInstance();
//        Thread.sleep(1000);
//        Calendar c4 = Calendar.getInstance();
//        System.out.println("c3 before c4:" + c3.before(c4));
//        System.out.println("c3 after c4:" + c3.after(c4));
//        c3.add(Calendar.DAY_OF_YEAR, 1);
//        System.out.println("c3 after c4:" + c3.after(c4) + "\n");

        //时间，替代Calendar、Date
        LocalDateTime localDateTime = LocalDateTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        LocalDateTime localDateTime2 = LocalDateTime.now();
        System.out.println("source time:" + localDateTime);
        System.out.println("time1 before time2:" + localDateTime.isBefore(localDateTime2));
        System.out.println("time1 after time2:" + localDateTime.isAfter(localDateTime2));
        //不改变原始值
        System.out.println("time add 1 day:" + localDateTime.plusDays(1));
        System.out.println("source time:" + localDateTime);
        //替代SimpleDateFormat
//        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String strDate = UtilForObject.formatForDateTime(localDateTime);
        System.out.println("format datetime:" + strDate);
        localDateTime = UtilForObject.parseForDateTime(strDate);
        System.out.println("parse datetime:" + localDateTime);
        strDate = UtilForObject.formatForDate(localDateTime);
        System.out.println("format date:" + strDate);
        strDate = UtilForObject.formatForTime(localDateTime);
        System.out.println("format time:" + strDate + "\n");

        Instant start = Instant.parse("2022-10-03T10:15:30.00Z");
        Instant end = Instant.parse("2022-10-03T10:16:30.00Z");
        // start -> end
        Duration duration = Duration.between(start, end);
        //如果为true表示开始时间晚于结束时间
        if (!duration.isNegative()) {
            //获取时间间隔
            System.out.println("获取时间段的秒数：" + duration.getSeconds());
        }
        System.out.println();

        //期间
        long current = System.currentTimeMillis();
        Duration millis = Duration.ofMillis(current);
        System.out.println(current + "->" + millis.plusDays(1).toMillis() + "\n");
    }

    /*
    对象转换
    简单的使用反射机制性能并没有问题
    copy objects time:170ms
    userVO = UserVO(userCode=0001, userName=张三)
    userVO list = [UserVO(userCode=0001, userName=张三), UserVO(userCode=0002, userName=李四), UserVO(userCode=0003, userName=王五)]
     */
    public static void main7(String[] args) {
        User u1 = User.builder().userCode("0001").userName("张三").userAge(18).build();

        int n = 1000;
        UserVO userVO = null;
        long start = System.currentTimeMillis();
        for (int i = 0; i < n; i++) {
            userVO = UtilForBean.beanCopy(u1, UserVO.class);
        }
        long end = System.currentTimeMillis();
        System.out.println("copy objects time:" + (end - start) + "ms");
        System.out.println("userVO = " + userVO);

        List<UserVO> list = UtilForBean.deepBeanCopy(Main02_2_Collection.USERS1, UserVO.class);
        System.out.println("userVO list = " + list);
    }

}
